package com.tyut.utils;

import com.tyut.domain.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;




public class HtmlParseUtil {

    public List<Content> parseMusic(String Keywords) throws Exception {
        String url =""+Keywords;
        //解析html
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("songlist");
        System.out.println(element.html());
        Elements elements = element.getElementsByTag("li");
        ArrayList<Content> musicList = new ArrayList<>();
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String title = el.getElementsByClass("songlist").eq(0).text();
            String singer = el.getElementsByClass("artist").eq(0).text();
            Content content = new Content();
            content.setImg(img);
            content.setTitle(title);
            content.setSinger(singer);
            musicList.add(content);

        }
        return musicList;

    }

}
